package com.example.mycarcalling.service.impl;

import com.example.mycarcalling.bean.Guide;
import com.example.mycarcalling.bean.Order;
import com.example.mycarcalling.dao.GuideMapper;
import com.example.mycarcalling.dao.OrderMapper;
import com.example.mycarcalling.service.GuideService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service("guideService")
@Transactional(rollbackFor = Exception.class)
public class GuideServiceImpl implements GuideService {

    @Resource
    private GuideMapper guideMapper;

    @Resource
    private OrderMapper orderMapper;

    @Override
    public Long savaGuideInfo(Guide guide) {
        if(null == guide){
            return null;
        }
        Guide guide1 = null;
        try {
            System.out.println("执行savaGuide");
            guide1 = guideMapper.selectGuideByOrderDriver(guide.getGuiOrder());
            if(null != guide1){
                //存在则更新
                guide.setGuideId(guide1.getGuideId());
                guideMapper.updateByPrimaryKeySelective(guide);
                return guide.getGuideId();
            }
            //不存在则插入
            guideMapper.insertSelective(guide);
            System.out.println("执行savaGuide");
            return guide.getGuideId();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Guide getDriverGuide(Long orderId) {
        if(0 == orderId){
            return null;
        }
        Guide guide;
        try {
            guide = guideMapper.selectGuideByOrder(orderId);
            return guide;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
